package com.windy.myvideo;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.media.ThumbnailUtils;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.windy.myvideo.model.VideoInfo;

import java.util.List;

/**
 * Created by dingfeng on 2016/9/16.
 */
public class VideoAdapter extends BaseAdapter {

    Context mContext;
    List<VideoInfo> mVideoList;

    public VideoAdapter(Context context, List<VideoInfo> list) {
        mContext = context;
        mVideoList = list;
    }

    @Override
    public int getCount() {
        return mVideoList.size();
    }

    @Override
    public Object getItem(int position) {
        return mVideoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        VideoInfo item = mVideoList.get(position);
        ViewHolder holder;
        if (null == convertView) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_video, null);
            holder = new ViewHolder();
            holder.imgThumb = (ImageView) convertView.findViewById(R.id.imgThumb);
            holder.txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
//        Bitmap thumbnail = ThumbnailUtils.createVideoThumbnail(item.mPath, MediaStore.Video.Thumbnails.FULL_SCREEN_KIND);
//        Bitmap thumbnail = getVideoThumbnail(item.mPath);
//        if (thumbnail != null) {
//            holder.imgThumb.setImageBitmap(thumbnail);
//        }
//        Glide.with(mContext).load(ThumbnailUtils.createVideoThumbnail(item.mPath, MediaStore.Video.Thumbnails.FULL_SCREEN_KIND))
//                .asBitmap()
//                .placeholder(R.drawable.default_thumb)
//                .into(holder.imgThumb);

        holder.txtTitle.setText(item.mName);

        return convertView;
    }

    class ViewHolder {
        ImageView imgThumb;
        TextView txtTitle;
    }

    public Bitmap getVideoThumbnail(String filePath) {
        Bitmap bitmap = null;
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            retriever.setDataSource(filePath);
            bitmap = retriever.getFrameAtTime();
        }
        catch(IllegalArgumentException e) {
            e.printStackTrace();
        }
        catch (RuntimeException e) {
            e.printStackTrace();
        }
        finally {
            try {
                retriever.release();
            }
            catch (RuntimeException e) {
                e.printStackTrace();
            }
        }
        return bitmap;
    }
}
